<?php 
namespace app\api\controller;

use think\Controller;
use think\facade\Validate;
use think\Db;

/**
 * @title 首页以及汽车圈接口
 */
class Car extends Controller
{
	/**
	 * @title 广告
	 * @description 广告
	 * @url /api/advert
	 * @method post
	 *
	 * @return data:返回数据@
	 * @data id:广告ID;type:类型1=售卖，2=求购，3=外部链接;image:图片;target:目标地址
	 * 
	 */
	public function advert()
	{
		$res = model('Advert')->field('id,type,image,target')
			->withAttr('image',function($value,$data){
				return path_deal_with($value);
			})
			->where('is_show',1)->order('weigh asc')->select();

		$this->result(['data'=>$res],1,'获取成功','json');
	}

	/**
	 * @title 搜索
	 * @description 搜索
	 * @url /api/search_list
	 * @method post
	 *
	 * @param name:search type:string require:1 desc:搜索关键词
	 * @return data:返回数据@
	 * @data id:发布ID;type:类型1=售卖，2=求购;title:标题;price:价格;price_type:价格类型;brand_id:品牌id;model_id:车型id;car_img:图片;car_brand:品牌;car_model:车型
	 * 
	 */
	public function search_list()
	{
		$search = input('search');
		if(!$search){
			$this->result('',0,'请输入搜索关键词','json');
		}

		$map[] = ['title','like','%'.$search.'%'];

		$res = model('Car')
			->field('id,type,title,price,price_type,brand_id,model_id')
			->withAttr('type',function($value,$data){
				return $value == 1 ? '售卖' : '求购';
			})
			->withAttr('price_type',function($value,$data){
				return $value == 1 ? '内' : '起';
			})
			->withAttr('car_img',function($value,$data){
				$car_img = model('CarImg')->where('car_id',$data['id'])->find();
				return $car_img ? path_deal_with($car_img->image) : '';
			})
			->withAttr('car_brand',function($value,$data){
				return model('CarBrand')->where('id',$data['brand_id'])->value('brand_name');
			})
			->withAttr('car_model',function($value,$data){
				return model('CarModel')->where('id',$data['model_id'])->value('model_name');
			})
			->where($map)
			->order('create_time desc')
			->append(['car_img,car_brand,car_model'])
			->paginate(10,false,['query'=>input()]);

		//保存搜索记录
		if(cookie('user_id')){
			$data = [
				'user_id' => cookie('user_id'),
				'keyword' => $search
			];

			model('Search')->save($data);
		}
		$this->result(['data'=>$res->items(),'total'=>$res->total()],1,'获取成功','json');

	}

	/**
	 * @title 历史搜索
	 * @description 历史搜索
	 * @url /api/search_history
	 * @method post
	 *
	 * @return data:返回数据@
	 * @data id:搜索ID;keyword:关键词
	 * 
	 */
	public function search_history()
	{
		$user_id = cookie('user_id');

		$res = model('Search')->field('id,keyword')->where('user_id',$user_id)->select();

		$this->result(['data'=>$res],1,'获取成功','json');
	}

	/**
	 * @title 删除历史搜索
	 * @description 删除历史搜索
	 * @url /api/delete_search_history
	 * @method post
	 *
	 */
	public function delete_search_history()
	{
		$user_id = cookie('user_id');

		$res = model('Search')->where('user_id',$user_id)->useSoftDelete('delete_time',time())->delete();
		if($res){
			$this->result('',1,'删除成功','json');
		}else{
			$this->result('',0,'删除失败','json');
		}
	}

	/**
	 * @title 品牌
	 * @description 品牌
	 * @url /api/car_brand
	 * @method post
	 *
	 * @return data:返回数据@
	 * @data id:品牌id;brand_name:品牌名称
	 * 
	 */
	public function car_brand()
	{
		$res = model('CarBrand')->field('id as value,brand_name as text')->where('status',1)->order('weigh asc')->select();

		$this->result(['data'=>$res],1,'获取成功','json');
	}

	/**
	 * @title 车型
	 * @description 车型
	 * @url /api/car_model
	 * @method post
	 *
	 * @return data:返回数据@
	 * @data id:车型id;model_name:车型名称
	 * 
	 */
	public function car_model()
	{
		$res = model('CarModel')->field('id as value,model_name as text')->where('status',1)->order('weigh asc')->select();

		$this->result(['data'=>$res],1,'获取成功','json');
	}

	/**
	 * @title 发布
	 * @description 发布
	 * @url /api/release
	 * @method post
	 *
	 * @param name:type type:integer require:1 desc:类型1=售卖2=求购
	 * @param name:title type:integer require:1 desc:标题
	 * @param name:price type:string require:1 desc:价格
	 * @param name:price_type type:integer require:1 desc:价格类型1=内2=起
	 * @param name:brand_id type:integer require:0 desc:品牌id
	 * @param name:model_id type:integer require:0 desc:车型id
	 * @param name:brief type:integer require:1 desc:詳情
	 * @param name:car_img[] type:file require:1 desc:图片
	 * 
	 */
	public function release()
	{
		$user_id = cookie('user_id');

		$data = [
			'type' => input('type',1),
			'title' => input('title'),
			'price' => input('price'),
			'price_type' => input('price_type'),
			'brief' => input('brief'),
			'car_img' => request()->file('car_img')
		];
		if($data['type'] == 1){
			$data['brand_id'] = input('brand_id');
			$data['model_id'] = input('model_id');
		}

		$rule = [
			'type' => 'require',
			'title' => 'require',
			'price' => 'require',
			'price_type' => 'require',
			'brief' => 'require',
			'car_img' => 'require'
		];
		if($data['type'] == 1){
			$rule['brand_id'] = 'require';
			$rule['model_id'] = 'require';
		}

		$msg = [
			'type.require' => '请选择发布类型',
			'title.require' => '请输入标题',
			'price.require' => '请输入价格',
			'price_type.require' => '请选择价格类型',
			'brief.require' => '请输入详情',
			'car_img.require' => '请上传图片'
		];
		if($data['type'] == 1){
			$msg['brand_id.require'] = '请选择品牌';
			$msg['model_id.require'] = '请选择车型';
		}

		$validate = Validate::make($rule,$msg);
		if(!$validate->check($data)){
			$this->result('',0,$validate->getError(),'json');
		}

		$car_img = $data['car_img'];
		$data['user_id'] = $user_id;
		unset($data['car_img']);

		Db::startTrans();
		try{
			$res = model('Car')->create($data);
			if($car_img){
				foreach ($car_img as $key => $value) {
					$car_img_data[$key] = [
						'car_id' => $res->id,
						'image' => upload_image($value)
					];
				}

				model('CarImg')->saveAll($car_img_data);
			}
			Db::commit();
		}catch(\Exception $e){
			Db::rollback();
			$this->result('',0,$e->getMessage(),'json');
		}

		$this->result('',1,'发布成功','json');
	}
	
	/**
	 * @title 获取汽车展示列表
	 * @description 获取汽车展示列表
	 * @url /api/car_list
	 * @method post
	 *
	 * @param name:type type:integer require:1 desc:类型1=售卖2=求购
	 * @param name:brand_id type:integer require:0 desc:品牌id
	 * @param name:model_id type:integer require:0 desc:车型id
	 * @param name:price type:integer require:0 desc:价格;0=不限，1=5万以下，2=5-10万，3=10-15万，4=15-20万，5=20-25万，6=25-30万，7=30万以上
	 *
	 * @data id:发布ID;type:类型1=售卖，2=求购;title:标题;price:价格;price_type:价格类型;brand_id:品牌id;model_id:车型id;car_img:图片;car_brand:品牌;car_model:车型
	 * 
	 */
	public function car_list()
	{
		$type = input('type',1);
		$brand_id = input('brand_id',0);
		$model_id = input('model_id',0);
		$price = input('price',0);

		$map = [];
		if($type){
			$map[] = ['type','=',$type];
		}
		if($brand_id){
			$map[] = ['brand_id','=',$brand_id];
		}
		if($model_id){
			$map[] = ['model_id','=',$model_id];
		}
		if($price){
			switch ($price) {
				case '1':
					$map[] = ['price','between',[0,5]];
					break;
				case '2':
					$map[] = ['price','between',[5,10]];
					break;
				case '3':
					$map[] = ['price','between',[10,15]];
					break;
				case '4':
					$map[] = ['price','between',[15,20]];
					break;
				case '5':
					$map[] = ['price','between',[20,25]];
					break;
				case '6':
					$map[] = ['price','between',[25,30]];
					break;
				default:
					$map[] = ['price','>',30];
					break;
			}
		}

		$res = model('Car')
			->field('id,type,title,price,price_type,brand_id,model_id')
			->withAttr('type',function($value,$data){
				return $value == 1 ? '售卖' : '求购';
			})
			->withAttr('price_type',function($value,$data){
				return $value == 1 ? '内' : '起';
			})
			->withAttr('car_img',function($value,$data){
				$car_img = model('CarImg')->where('car_id',$data['id'])->find();
				return $car_img ? path_deal_with($car_img->image) : '';
			})
			->withAttr('car_brand',function($value,$data){
				return model('CarBrand')->where('id',$data['brand_id'])->value('brand_name');
			})
			->withAttr('car_model',function($value,$data){
				return model('CarModel')->where('id',$data['model_id'])->value('model_name');
			})
			->where($map)
			->order('create_time desc')
			->append(['car_img','car_brand','car_model'])
			->paginate(10,false,['query'=>input()]);

		$this->result(['data'=>$res->items(),'total'=>$res->total()],1,'获取成功','json');
	}

	/**
	 * @title 发布用户信息
	 * @description 发布用户信息
	 * @url /api/user_release_info
	 * @method post
	 *
	 * @param name:user_id type:integer require:1 desc:用户id
	 * 
	 * @return data:返回数据@
	 * @data user_id:用户id;username:用户昵称;avatar:头像
	 * 
	 */
	public function user_release_info()
	{
		$user_id = (int)input('user_id');

		$find = model('User')::get($user_id);
		if(!$find){
			$this->result('',0,'用户不存在','json');
		}

		$userinfo = model('User')::get($user_id)->withAttr('avatar',function($value,$data){
			return path_deal_with($value);
		});

		$this->result(['user_id'=>$userinfo->id,'username'=>$userinfo->username,'avatar'=>$userinfo->avatar],1,'获取成功','json');
	}

	/**
	 * @title 用户发布列表
	 * @description 用户发布列表
	 * @url /api/user_release_list
	 * @method post
	 *
	 * @param name:user_id type:integer require:1 desc:用户id
	 * 
	 * @return data:返回数据@
	 * @data id:发布id;type:分布类型;title:标题;price:价格;brand_id:品牌id;model_id:车型id;car_img:图片;car_brand:品牌名称;car_model:车型名称
	 * 
	 */
	public function user_release_list()
	{
		$user_id = (int)input('user_id');

		$map[] = ['user_id','=',$user_id];

		$res = model('Car')
			->field('id,type,title,price,price_type,brand_id,model_id')
			->withAttr('type',function($value,$data){
				return $value == 1 ? '售卖' : '求购';
			})
			->withAttr('car_img',function($value,$data){
				$car_img = model('CarImg')->where('car_id',$data['id'])->find();
				return $car_img ? path_deal_with($car_img->image) : '';
			})
			->withAttr('car_brand',function($value,$data){
				return model('CarBrand')->where('id',$data['brand_id'])->value('brand_name');
			})
			->withAttr('car_model',function($value,$data){
				return model('CarModel')->where('id',$data['model_id'])->value('model_name');
			})
			->where($map)
			->order('create_time desc')
			->append(['car_img','car_brand','car_model'])
			->paginate(10,false,['query'=>input()]);

		$this->result(['data'=>$res->items(),'total'=>$res->total()],1,'获取成功','json');
	}

}
 ?>